define([
	'app',
	'jquery',
	'lodash',
	"resources/ContactsResource",
	'services/TipsService'
], function(app, _ ) {

	var STATUS_ENABLE = 0;
	var STATUS_DISABLE = 1;

	var deps = [ '$state', "$timeout", "$document", 'ContactsResource', 'TipsService' ];

	function directive($state, $timeout, $document, ContactsResource, TipsService) {
		return {
			templateUrl: 'views/group/GroupUserItems.html',
			replace: true,
			scope: {
				member: '=fcGroupUserItems',
				group:'=?'
			},
			link: function($scope) {
				
				/**
				 * 显示用户详情框
				 */
				$scope.showMessages = function(event, userId) {
					$scope.$emit("getUserId", userId); //事件广播，将事件冒泡传递给父controller
					ContactsResource.getUserByUserId(userId).success(function(user){
		        		//显示详细信息框
						var objDiv = $("#mydiv1"); 
						if(event) {
							var target = $(event.currentTarget),
							position = target.position();
							if (user.userId) {
								if(position.top < 320){
									$(objDiv).css("top",  "20px"); 
								}else{
									$(objDiv).css("top", (position.top - 313) + "px"); 
								}
								$(objDiv).css("left", (position.left) + "px"); 
							} else {
								if (position.top < 60) {
									$(objDiv).css("top",  "20px"); 
								} else {
									$(objDiv).css("top", (position.top - 60) + "px"); 
								}
								$(objDiv).css("left", position.left + "px"); 
							} 
						}
						objDiv.fadeToggle();
						event.stopPropagation(); //停止事件冒泡
					});
				};
				
				
				/**
				 * 删除成员
				 */
				$scope.remove = function(userId) {
					var removeUserIds = [userId];
					if ($scope.group.isManager) { //是管理员才能删除，调用删除接口
						ContactsResource.delGroupUser($scope.group.groupId,removeUserIds).success(function(){
							TipsService.show("<span class='glyphicon glyphicon-ok'></span>&nbsp;删除成功");
							$scope.member.hidden = true;
							$scope.$emit("refreshList"); //事件广播，刷新列表
		                });
					}
					event.stopPropagation(); //停止事件冒泡
				};
			}
		};
	}

	directive.$inject = deps;
	return app.lazy.directive('fcGroupUserItems', directive);
});